﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Surface.Presentation;
using ScrumTable.UI.View.Command;
using ScrumTable.UI.View.Controls;
using ScrumTable.UI.ViewModel;
using ScrumTable.UI.ViewModel.Data;

namespace ScrumTable.UI.View.WinTouch.UserControls.SubControls
{
	/// <summary>
    /// Interaction logic for AddChildTaskControl.xaml
	/// </summary>
    public partial class VelocityChart : IDropUserControl
	{


        private readonly Predicate<object> _canDropOnVelocity = item => item is UserStoryViewModel || item is BugViewModel;


        /// <summary>
        /// Property for CanDropOnVelocity
        /// </summary>
        public Predicate<object> CanDropOnVelocity
        {
            get { return _canDropOnVelocity; }
        }

        public VelocityChart()
        {
            InitializeComponent();
        }

        private void OnVelocityDrop(object sender, SurfaceDragDropEventArgs e)
        {
            var control = (Control)(sender);
            var data = control.DataContext as VelocityIterationData;
            if (data != null)
            {
                if (e.Cursor.Data is UserStoryViewModel)
                {
                    var item = ((UserStoryViewModel)e.Cursor.Data).Original;
                    if (!data.Iteration.Stories.Contains(item))
                    {
                        data.Iteration.Stories.Add(item);
                    }
                }
                else if (e.Cursor.Data is BugViewModel)
                {
                    var item = ((BugViewModel)e.Cursor.Data).Original;
                    if (!data.Iteration.Bugs.Contains(item))
                    {
                        data.Iteration.Bugs.Add(item);
                    }
                }
            }
        }

        private void OnItemMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CommandLib.SelectVelocityItem.Execute(((Control)sender).DataContext, this);
            e.Handled = true;
        }

        private void OnItemTouchDown(object sender, System.Windows.Input.TouchEventArgs e)
        {
            CommandLib.SelectVelocityItem.Execute(((Control)sender).DataContext, this);
            e.Handled = true;
        }

	    public FrameworkElement GetRootControl
	    {
            get { return this; }
	    }

	    public bool ShowIconWithbackground
	    {
            get { return false; }
	    }

	    public CanDropResult CanDrop(object objectToDrop)
	    {
	        return CanDropResult.None;
	    }
	}
}